﻿using Super_Management_Mayhem.Content.Animations;
using Super_Management_Mayhem.Content.Spritesheet;
using Transformable_Engine_v2.Engine;
using Transformable_Engine_v2.Engine.Animation.FrameAnimation;
using Transformable_Engine_v2.Engine.GameObjects.Containers;
using Transformable_Engine_v2.Engine.GameObjects.Primitives;
using Transformable_Engine_v2.Engine.Graphics;
using Transformable_Engine_v2.Engine.Helper;
using Transformable_Engine_v2.Engine.Time;
using Transformable_Engine_v2.Engine.Transformable_Math.Geometry;

namespace Super_Management_Mayhem.Screens.Playing.Items
{
    public class Warp : Container
    {
        private TE_Vector2 _targetPosition;
        private TE_Rectangle _boundingBox;
        private Container _starLayer;

        private Timer _warpStarTimer;

        private Container _warpAnimationContainer;

        public new TE_Rectangle BoundingBox
        {
            get 
            { 
                return _boundingBox + Position; 
            }
            private set { _boundingBox = value; }
        }

        public TE_Vector2 WarpPlayer()
        {
            var x = new ImageContainer {
                                            Position = new TE_Vector2(0.5f, 0.75f).ToGridbound() + AbsolutePosition,
                                            Sprite = ItemSheet.GetSprite(ItemSheet.PlayerWarp.Warp0),
                                            Origin = TE_Rectangle.Alignment.BottomMiddle,
                                            Color = new Color(Color.White, 200)
                                            //Visible = true
                                        };
            x.AddAnimations(new FrameAnimation(new PlayerWarp()));
            Playscreen.Map.AddParticle(x);

            return _targetPosition;
        }

        public TE_Vector2 WarpItem()
        {
            //todo: add item warp animation
            return _targetPosition;
        }

        public Warp(Color warpColor, TE_Vector2 targetPosition)
        {
            _targetPosition = targetPosition.ToGridbound();
            BoundingBox = new TE_Rectangle(0, 0, 16, 16);
            var item = new ImageContainer
            {
                Sprite = ItemSheet.GetSprite(ItemSheet.Warp.Warp0),
                Color = warpColor
            };
            item.AddAnimations(new FrameAnimation(new WarpAnimation()));

            _starLayer = new Container();
            _warpAnimationContainer = new Container();

            AddChildren(item, _starLayer);

            if (Playscreen.SHOW_COLLISION)
            {
                AddChildren(new RectanglePrimitive
                    {
                        Shape = new TE_Rectangle(0, 0, 1, 1),
                        FillColor = new Color(Color.Green, 100)
                    });
            }
            
            _warpStarTimer = new Timer(Randomizer.Int(100, 300), false, SpawnStar);
            _warpStarTimer.Start();
            AddTimers(_warpStarTimer);

            
        }

        private void SpawnStar()
        {
            _warpStarTimer.Reset(Randomizer.Int(100, 300));
            float scaleSeed = (float) Randomizer.Double(0.5, 1.5);
            float positionSeed = (float) Randomizer.Double(0, 1);
            _starLayer.AddChildren(new WarpStar(new TE_Vector2(scaleSeed, scaleSeed))
            {
                PositionGridBound = new TE_Vector2(positionSeed, 0.5f)
            });
        }

        public override void Draw()
        {
            base.Draw();
        }
    }
}